package com.aimaterials.resource.controller;

import java.io.IOException;
import java.util.List;

import com.aimaterials.common.core.domain.entity.SysUser;
import com.aimaterials.common.core.domain.model.LoginUser;
import com.aimaterials.common.utils.ServletUtils;
import com.aimaterials.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.aimaterials.common.annotation.Log;
import com.aimaterials.common.core.controller.BaseController;
import com.aimaterials.common.core.domain.AjaxResult;
import com.aimaterials.common.enums.BusinessType;
import com.aimaterials.resource.domain.ResResource;
import com.aimaterials.resource.service.IResResourceService;
import com.aimaterials.common.utils.poi.ExcelUtil;
import com.aimaterials.common.core.page.TableDataInfo;

/**
 * 资源Controller
 * 
 * @author DaemonSu
 * @date 2020-11-10
 */
@RestController
@RequestMapping("/resource/resource")
public class ResResourceController extends BaseController
{
    @Autowired
    private IResResourceService resResourceService;

    @Autowired
    private TokenService tokenService;




    /**
     * 查询资源列表
     */
    @PreAuthorize("@ss.hasPermi('resource:resource:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResResource resResource)
    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        SysUser user = loginUser.getUser();
//        resResource.setSchoolId(user.getDeptId());
        startPage();
        List<ResResource> list = resResourceService.selectResResourceList(resResource);
        return getDataTable(list);
    }


    /**
     * 导出资源列表
     */
    @PreAuthorize("@ss.hasPermi('resource:resource:export')")
    @Log(title = "资源", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ResResource resResource)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resResource.setSchoolId(user.getDeptId());
        List<ResResource> list = resResourceService.selectResResourceList(resResource);
        ExcelUtil<ResResource> util = new ExcelUtil<ResResource>(ResResource.class);
        return util.exportExcel(list, "resource");
    }

    /**
     * 获取资源详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:resource:query')")
    @GetMapping(value = "/{resourceId}")
    public AjaxResult getInfo(@PathVariable("resourceId") Long resourceId)
    {
        return AjaxResult.success(resResourceService.selectResResourceById(resourceId));
    }

    /**
     * 新增资源
     */
    @PreAuthorize("@ss.hasPermi('resource:resource:add')")
    @Log(title = "资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResResource resResource)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resResource.setSchoolId(user.getDeptId());
        return toAjax(resResourceService.insertResResource(resResource));
    }

    /**
     * 修改资源
     */
    @PreAuthorize("@ss.hasPermi('resource:resource:edit')")
    @Log(title = "资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResResource resResource)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resResource.setSchoolId(user.getDeptId());
        return toAjax(resResourceService.updateResResource(resResource));
    }

    /**
     * 删除资源
     */
    @PreAuthorize("@ss.hasPermi('resource:resource:remove')")
    @Log(title = "资源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{resourceIds}")
    public AjaxResult remove(@PathVariable Long[] resourceIds)
    {
        return toAjax(resResourceService.deleteResResourceByIds(resourceIds));
    }
}
